package com.hk.service.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.google.common.collect.Lists;
import com.hk.domain.dto.SubDateDTO;
import com.hk.enums.YxbzEnum;
import com.hk.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CommonQueryBusiness {

    /**
     * 公共查询接口，适用于多张表有公共的字段的情况
     * bug: https://gitee.com/baomidou/mybatis-plus/issues/I3AZO6
     * @param entiyType
     * @param <T>
     * @return
     */
    public <T> List<T> getResult(List<SubDateDTO> list, Class service, T entiyType) {
        List list1 = null;
        try {
            if (CollectionUtils.isEmpty(list)) {
                return Lists.newArrayList();
            }
            IService iService = (IService) SpringContextUtil.getBean(service);
//            Class entity = SpringContextUtil.getEntity(service);
            //这个T只能传具体的对象：例如 new User();
            QueryWrapper<T> queryWrapper1 = new QueryWrapper<>();

            //SELECT username, password FROM user WHERE ( username = 'hkk' AND password = '1' ) OR ( username = 'hk' AND password = '22222222222' )
            list.stream().forEach(t -> {
                queryWrapper1.or(ts -> ts.eq("username", t.getUsername()).eq("password", t.getPassword()));
            });

//            queryWrapper1.and(queryWrapper ->
//                    list.stream().forEach(t -> {
//                        queryWrapper.or(ts -> ts.eq("username", t.getUsername()).eq("password", t.getPassword()));
//                    }));
//        QueryWrapper<Class> queryWrapper1 = new QueryWrapper<>();
//            queryWrapper1.setEntity(entity);
//        queryWrapper1.setEntityClass(entity);
            queryWrapper1.eq("Yxbz", YxbzEnum.Y.name());
            queryWrapper1.select("username, password");
            list1 = iService.list(queryWrapper1);
        } catch (Exception e) {
            log.error("异常原因={}", e);
        }
        if (CollectionUtils.isNotEmpty(list1)) {
            return list1;
        } else {
            return Lists.newArrayList();
        }
    }

}
